import {Service, aspect, log} from "@codefree/badger/service";
import operationLog from '../middleware/operation_log'

/**
 * Service 内置方法，需要modelMapping到数据库实体
 * create
 * delete
 * update
 * query
 *
 - this.ctx
 - this.ctx.model
 - this.ctx.config
 - this.otherService
 */
@aspect.modelMapping('Schedule')
@aspect.modelMapping('Major')
@operationLog(/Query$/i)
export default class ScheduleService extends Service {
    async updateDescription(scheduleResult) {

        let values = {
            schedule_start: scheduleResult.schedule_start,
            schedule_end: scheduleResult.schedule_end,
            prac_dline: scheduleResult.prac_dline,
            test_dline: scheduleResult.test_dline,
            schedule_scores: scheduleResult.schedule_scores,
        };
        let option = {
            where: {
                id: scheduleResult.id
            }
        };
        // update Dictionary set description = @description where id = @id
        await this.ctx.model.Schedule.update(values, option)
    }

    async selfQuery(filter: Object) {

        const queryOption = {bind: {key: filter.id}};
        return this.pagination("SELECT m.major_name,s.id,s.schedule_start,s.schedule_end,s.test_dline,s.prac_dline FROM schedule s join major m on s.major_id = m.id  where s.teacher_id = $key", queryOption, 1, 10);
    }

    async queryClass(filter: Object){
        const listOption = {bind: {key:filter.id}};
        return this.pagination("SELECT * FROM major where teacher_id = $key",listOption);
    }  
    
    async queryScore(filter:object){
        
        const scoreOption = {bind: {key: filter}};
        
        return this.pagination("SELECT major_id,schedule_scores,schedule_start,schedule_end,test_dline,prac_dline FROM schedule where major_id = $key ",scoreOption);
     
        
    }
}
